<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import api from '@/utils/api';
import { useMenuStore } from '@/store/index';

const router = useRouter();
const menuStore = useMenuStore();

const confirm_disabled = ref(false);
const radio = ref('1');
const loginForm = reactive({
  no: '',
  name: '',
  password: '',
  rpassword: '',
  roleId: ''
});

const loginFormRef = ref();

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== loginForm.password) {
    callback(new Error('两次输入密码不一致!'));
  } else {
    callback();
  }
};

const rules = {
  no: [
    { required: true, message: '请输入管理员姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '2-10个字符', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入管理员姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '2-10个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入管理员密码', trigger: 'blur' },
    { min: 3, max: 16, message: '3-16个字符', trigger: 'blur' }
  ],
  rpassword: [
    { required: true, validator: validatePass, trigger: 'blur' },
    { min: 3, max: 16, message: '3-16个字符', trigger: 'blur' }
  ]
};

function login() {
  router.replace('/');
}

function confirm() {
  confirm_disabled.value = true;
  loginFormRef.value.validate((valid) => {
    if (valid) {
      if (radio.value === '1') loginForm.roleId = 1;
      if (radio.value === '2') loginForm.roleId = 2;
      api.post('/user/register', loginForm)
        .then((res) => {
          // 兼容后端返回格式
          if (res && res.code === 200) {
            // 可在此处存储用户信息到 pinia
            // menuStore.setMenu(res.menuList) // 如有需要
            router.replace('/');
          } else {
            confirm_disabled.value = false;
            alert('注册失败，请稍后重试！');
            return false;
          }
        })
        .catch(() => {
          confirm_disabled.value = false;
        });
    } else {
      confirm_disabled.value = false;
      console.log('校验失败');
      return false;
    }
  });
}
</script>


<template>
  <div class="loginBody">
    <div class="loginDiv">
      <div class="login-content">
        <h1 class="login-title">账号注册</h1>
        <el-form :model="loginForm" label-width="100px" :rules="rules" ref="loginFormRef">
          <el-form-item label="账号" prop="no">
            <el-input style="width: 200px" type="text" v-model="loginForm.no" autocomplete="off"
              size="small"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input style="width: 200px" type="text" v-model="loginForm.name" autocomplete="off"
              size="small"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input style="width: 200px" type="password" v-model="loginForm.password" show-password autocomplete="off"
              size="small" @keyup.enter="confirm"></el-input>
          </el-form-item>
          <el-form-item label="重复密码" prop="rpassword">
            <el-input style="width: 200px" type="password" v-model="loginForm.rpassword" show-password
              autocomplete="off" size="small" @keyup.enter="confirm"></el-input>
          </el-form-item>
          <el-form-item>
            <el-radio v-model="radio" label="1">管理员</el-radio>
            <el-radio v-model="radio" label="2">用户</el-radio>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="confirm" :disabled="confirm_disabled">注册</el-button>
            <el-button type="success" @click="login" :disabled="confirm_disabled">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>



<style scoped>
.loginBody {
  position: absolute;
  width: 100%;
  height: 100%;

  background-image: url(../assets/reg.jpeg);
  background-size: 100% 100%;
}

.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -250px;
  width: 400px;
  height: 400px;
  background: #fff;
  border-radius: 5%;
}

.login-title {
  margin: 20px 0;
  text-align: center;
}

.login-content {
  width: 400px;
  height: 250px;
  position: absolute;
  top: 25px;
  left: 25px;
}
</style>